<template>
    <div class="goods-list">
        <!-- <router-link class="goods-item" v-for="item in goodslist" :key="item.id + item.title" :to="'/home/goodsinfo/' + item.id" tag="div">
            <div class="item-header" >
                <img class="item-img" :src="item.img_url"> 
                <h3 class="item-title">{{ item.title }}</h3>
            </div>
            <div class="item-body">
                <p class="item-price">
                    <span class="new">￥{{ item.sell_price }}</span>
                    <span class="old">￥{{ item.market_price }}</span>
                </p>
                <p class="item-info">
                    <span>热卖中</span>
                    <span>剩{{ item.stock_quantity }}件</span>
                </p>
            </div>
        </router-link> -->

<!-- 在网页中，有两种跳转方式，方式一：使用a标签的形式，标签跳转  方式二：使用 window location.href的形式，叫做编程式导航 -->
        <div class="goods-item" v-for="item in goodslist" :key="item.id + item.title" @click="getDetail( item.id )">
            <div class="item-header" >
                <img class="item-img" :src="item.img_url"> 
                <h3 class="item-title">{{ item.title }}</h3>
            </div>
            <div class="item-body">
                <p class="item-price">
                    <span class="new">￥{{ item.sell_price }}</span>
                    <span class="old">￥{{ item.market_price }}</span>
                </p>
                <p class="item-info">
                    <span>热卖中</span>
                    <span>剩{{ item.stock_quantity }}件</span>
                </p>
            </div>
        </div>
        
        <mt-button type="danger" size="large" @click="getMore">加载更多</mt-button>
    </div>
</template>

<script>
export default {
 data(){
     return{
         pageindex: 1, //分页的页数
         goodslist:[]
     }
 },
 created(){
     this. getGoodsList()
 },
 methods:{
    getGoodsList(){
        this.$http.get("api/getgoods?pageindex=" + this.pageindex).then(result => {
            if(result.body.status === 0){
                this.goodslist = this.goodslist.concat(result.body.message)
            }
        })
    },
    getMore(){
        this.pageindex++
        this.getGoodsList()
    },
    getDetail(id){
        // 使用JS 方式进行路由导航
        // 注意： 一定要区分 this.$route  和  this.$router 这两个对象
        // 其中：this.$route 是【路由参数对象】，所有路由中的参数，params , query 都属于它
        //      this.$router 是一个【路由导航对象】，使用它可以方便使用
      
        // 1.最简单的
        // this.$router.push('/home/goodsinfo/' + id)
        // 2.传递对象
        // this.$router.push({path:'/home/goodsinfo/' + id})
        // 3.传递一个命名的路由
        this.$router.push({ name:"goodsinfo",params:{ id } })
    }
 }
}
</script>

<style lang="scss">
    .goods-list{
        display: flex;
        flex-wrap: wrap;
        justify-content: space-between;
        padding: 7px;

        .goods-item{
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            min-height: 288px;
            width: 49%;
            border:1px solid #ccc;
            box-shadow: 0 0 10px #ccc;
            margin: 4px 0;
            padding: 2px;
        }

        .item-header{

            .item-img{
               width: 100%;
            }

            .item-title{
                font-size: 14px;
            }
        }
        .item-body{
            background-color: #eee;
            p{
                margin: 0;
                padding: 4px;
            }
            .item-price{
                .new{
                    color: red;
                    font-weight: 700;
                    font-size: 16px;
                }
                .old{
                    text-decoration: line-through;
                    font-size: 12px; 
                    margin-left: 10px;
                }
            }
            .item-info{
                display: flex;
                justify-content: space-between;
                font-size: 12px;
            }
        }
    }
</style>